﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TVT.Common.Helpers;
using System.Data;

namespace Common.Helpers
{
    public class FileHelper
    {
        /// <summary>
        /// Renames the file to the same directory.
        /// </summary>
        /// <param name="name"> Complete path of current file name, includes extension.</param>
        /// <param name="newName">New file relative name, it doesn't include the path.</param>
        public static void Rename(String name, String newName)
        {
            String relativeName = name.Substring(name.LastIndexOf(WellKnownKeys.DirectoryDelim) + WellKnownKeys.DirectoryDelim.Length);
            String auxName = name.Replace(relativeName, newName);
            File.Copy(name, auxName, true);
            File.Delete(name);
        }

        public static void CreateFile(string fileName, byte[] file)
        {
            FileStream FileStream = new FileStream(fileName, FileMode.Create);
            FileStream.Write(file, 0, file.Length);
            FileStream.Close();
            FileStream.Dispose();
        }

        public static DataTable OpenDataTable(string file)
        {
            byte[] bytes = FileToArray(file);
            MemoryStream ms = new MemoryStream(bytes);
            DataSet ds = new DataSet();
            ds.ReadXml(ms);
            return ds.Tables[0];
        }

        public static byte[] FileToArray(string sFilePath)
        {
            System.IO.FileStream fs = new System.IO.FileStream(sFilePath,
                System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            br.Close();
            fs.Close();
            return bytes;
        }

        public static void Unzip(string fileName)
        {
            new ICSharpCode.SharpZipLib.Zip.FastZip().ExtractZip(fileName + ".zip", fileName, "");
        }

        public static void CompressZipFile(string zipFileName, string sourceDirectory)
        {
            new ICSharpCode.SharpZipLib.Zip.FastZip().CreateZip(zipFileName, sourceDirectory, false, ".bak$");
        }

    }
    public static class MyExtensions
    {
        #region object

        public static object GetValue(this object myObject, string propertyName)
        {
            return myObject.GetType().GetProperty(propertyName).GetValue(myObject, null);
        }

        #endregion

        #region string

        /// <summary>
        /// Parse the string into an array separating it by the given separator, also removes empty entries.
        /// </summary>
        /// <param name="sep"> Separator char of the array values</param>
        /// <returns></returns>
        public static string[] Split(this string value, char sep)
        {
            char[] separators = new char[1];
            separators[0] = sep;
            return value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string Remove(this string value, string toRemove)
        {
            string result = value;
            while (result.Contains(toRemove))
                result = result.Remove(result.LastIndexOf(toRemove), toRemove.Length);
            return result;
        }

        public static int CountAppearances(this string value, string toFind)
        {
            string aux = value;
            int count = 0;
            while (aux.Contains(toFind))
            {
                aux = aux.Remove(aux.LastIndexOf(toFind), toFind.Length);
                count++;
            }
            return count;
        }

        #endregion

        #region DateTime

        public static DateTime LastWorkingDate(this DateTime date)
        {
            return GetWorkingDate(date, -1);
        }

        public static DateTime NextWorkingDate(this DateTime date)
        {
            return GetWorkingDate(date, 1);
        }

        private static DateTime GetWorkingDate(DateTime date, int increase)
        {
            DateTime returnDate = date;
            bool isWorkingDate = false;

            while (!isWorkingDate)
            {
                switch (returnDate.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                    case DayOfWeek.Saturday:
                        returnDate = returnDate.AddDays(increase);
                        break;
                    default:
                        isWorkingDate = true;
                        break;
                }
            }
            return returnDate;
        }

        #endregion

    }
}
